#include "ejsqlmanager.h"

#include <QSqlError>
#include <QSqlQuery>
#include <QSqlDriver>

#include <QMessageBox>


#define DEL_PTR(p) if(p){delete p; p = nullptr;}

EJSqlManager *EJSqlManager::m_instance = new EJSqlManager;

EJSqlManager::~EJSqlManager()
{
    foreach (auto con, m_dbconn)
    {
        con.commit();
        con.close();
    }
}

bool EJSqlManager::open(const QString &passwd, const QString &path)
{
    QSqlDatabase dbconn = QSqlDatabase::addDatabase("QSQLITE");//SQLITE3   QSQLITE
    dbconn.setDatabaseName(path);
    //dbconn.setPassword(passwd);
    //dbconn.setConnectOptions("QSQLITE_CREATE_KEY");

    if (!dbconn.open())
    {
        QMessageBox::warning(nullptr, QObject::tr("警告"), QObject::tr("打开数据库失败"), QMessageBox::Ok);
        return false;
    }

    m_dbconn.insert(path, dbconn);

    return true;
}

bool EJSqlManager::query(const QString &sql, const QString &path)
{

    return false;
}

bool EJSqlManager::createTable(const QStringList &column, const QString &table, const QString &path)
{
    bool ret = false;

    do
    {
        auto it = m_dbconn.find(path.length() == 0 ? m_defaultDB : path);

        if (it == m_dbconn.end()) break;

        if (column.empty())
        {
            QMessageBox::warning(nullptr, QObject::tr("警告"), QObject::tr("表名为空, 或者没有提供列名"), QMessageBox::Ok);
            break;
        }

        QSqlQuery query(it.value());

        QString col = "";

        for (auto it = column.begin(); it != column.end(); ++it)
        {
            col = col + (*it).toUtf8().data() + QObject::tr(" varchar2(32), ");
        }

        col = col.remove(col.length() - 2, 2);

        auto tb = table.length() == 0 ? m_defaultTB : table;

        QString sql = QString().sprintf("CREATE TABLE %s ( %s )", tb.toUtf8().data(), col.toUtf8().data());

        if (!query.exec(sql))
        {
            QString errInfo = QObject::tr("执行sql语句失败[") + sql + QObject::tr("]");

            QMessageBox::warning(nullptr, QObject::tr("警告"), errInfo, QMessageBox::Ok);
            break;
        }

        it.value().commit();

        ret = true;

    }while(0);

    return ret;
}

bool EJSqlManager::existTable(const QString &table, const QString &path)
{
    bool ret = false;

    do
    {
        auto it = m_dbconn.find(path.length() == 0 ? m_defaultDB : path);

        if (it == m_dbconn.end()) break;

        QSqlQuery query(it.value());

        auto tb = table.length() == 0 ? m_defaultTB : table;

        QString sql = QString().sprintf("SELECT * FROM sqlite_master WHERE type='table' AND name='%s'", tb.toUtf8().data());

        if (!query.exec(sql))
        {
            QString errInfo = QObject::tr("执行sql语句失败[") + sql + QObject::tr("]");

            QMessageBox::warning(nullptr, QObject::tr("警告"), errInfo, QMessageBox::Ok);
            break;
        }

        if (!query.next()) break;

        ret = true;

    }while(0);

    return ret;
}

QSqlTableModel *EJSqlManager::getTableModel(const QString &table, const QString &path)
{
    QSqlTableModel *model = nullptr;

    do
    {
        auto it = m_dbconn.find(path.length() == 0 ? m_defaultDB : path);

        if (it == m_dbconn.end()) break;

        auto tb = table.length() == 0 ? m_defaultTB : table;

        model = new QSqlTableModel(nullptr, it.value());
        model->setTable(tb);
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);

        if (!model->select()) DEL_PTR(model)

    }while(0);

    return model;

}
